<?php

/*
  Copyright (c) 2005 Kreditor AB

  Released under the GNU General Public License
*/

// INCLUDE THE LANGUAGE SUPPORT FILE
if(strpos($_SERVER['SCRIPT_FILENAME'],'admin'))
    @include_once '../'.DIR_WS_CLASSES . 'kreditor_lang.php';
else
    @include_once DIR_WS_CLASSES . 'kreditor_lang.php';

class kreditor
{
    var $code, $title, $description, $enabled;

	// class constructor
    function kreditor()
    {
		
		global $order;

		$this->code = 'kreditor';
		$this->title = MODULE_PAYMENT_KREDITOR_TEXT_TITLE;
		$this->description = MODULE_PAYMENT_KREDITOR_TEXT_DESCRIPTION;
		$this->enabled = ((MODULE_PAYMENT_KREDITOR_STATUS == 'True') ?
			true : false);

		if(isset($order)){
	  		$currency = $order->info['currency'];

	  		if ($currency != "SE" && $currency != "se" && $currency != "SEK" && $currency != "sek")
	    		$this->enabled = false;
		}

		if ($order->info['total']*$order->info['currency_value'] > MODULE_PAYMENT_KREDITOR_ORDER_LIMIT)
	    	$this->enabled = false;

		$this->sort_order = MODULE_PAYMENT_KREDITOR_SORT_ORDER;

		if ((int)MODULE_PAYMENT_KREDITOR_ORDER_STATUS_ID > 0) {
	    	$this->order_status = MODULE_PAYMENT_KREDITOR_ORDER_STATUS_ID;
		}

		if (is_object($order))
	    	$this->update_status();
    }

// class methods
    function update_status() {
	global $order, $db;

	if ($this->enabled == true && (int)MODULE_PAYMENT_KREDITOR_ZONE > 0) {
	    $check_flag = false;
	    $check = $db->Execute("select zone_id from " .
					TABLE_ZONES_TO_GEO_ZONES .
					" where geo_zone_id = '" .
					MODULE_PAYMENT_KREDITOR_ZONE .
					"' and zone_country_id = '" .
					$order->billing['country']['id'] .
					"' order by zone_id");

	    // found and fixed by Payam Karimian
	    while (!$check->EOF) {
		if ($check->fields['zone_id'] < 1) {
		    $check_flag = true;
		    break;
		} elseif ($check->fields['zone_id'] == $order->billing['zone_id']) {
		    $check_flag = true;
		    break;
		}
		$check->MoveNext();
	    }

	    if ($check_flag == false) {
		$this->enabled = false;
	    }
	}
    }

    function javascript_validation() {
	return false;
    }

    function selection() {
	global $order, $db;

	$find_personnummer =
	    $db->Execute("show columns from " . TABLE_CUSTOMERS);

	$has_personnummer = false;
	$has_dob = false;

	while(!$find_personnummer->EOF) {
	    if ($find_personnummer->fields['Field']=="customers_personnummer")
		$has_personnummer = true;
	    if ($find_personnummer->fields['Field']=="customers_dob")
		$has_dob = true;
	    $find_personnummer->MoveNext();
	}

	if ($has_personnummer) {
	    $customer =
		$db->Execute("select customers_personnummer from " .
			     TABLE_CUSTOMERS . " where customers_id = '".
			     (int)$_SESSION['customer_id']."'");
	    
	    $personnummer = $customer->fields['customers_personnummer'];
	}
	else if ($has_dob) {
	    $customer =
		$db->Execute("select customers_dob from " .
			     TABLE_CUSTOMERS . " where customers_id = '".
			     (int)$_SESSION['customer_id']."'");
	    // $customer = zen_db_fetch_array($customer_query);
	    
	    $personnummer = substr($customer->fields['customers_dob'],2);
	}
	else {
	    $personnummer = "";
	}

	if (MODULE_PAYMENT_KREDITOR_PRE_POPULATE == "False")
	    $personnummer = "";

        // FIX THE LINK TO THE CONDITION
        if(is_numeric(MODULE_KREDITOR_PAYMENT_CHG_FIXED) && MODULE_KREDITOR_PAYMENT_CHG_FIXED >= 0)
            $kreditor_payment_chg = MODULE_KREDITOR_PAYMENT_CHG_FIXED*$order->info['currency_value'];
        else
            $kreditor_payment_chg = 0;

            $paymeny_charge_link = "<a href=\"javascript:void();\" onclick=\"window." . 
                "open('https://www.kreditor.se/villkor.yaws?eid=" . 
                MODULE_PAYMENT_KREDITOR_EID."&charge=" . 
                $kreditor_payment_chg."'," . 
                "'villkor','width=300,height=300,scrollbars=no');\">" .
                MODULE_PAYMENT_KREDITOR_TERMS."</a>";
	    
	return array('id' => $this->code,
		     'module' => $this->title,
		     'fields' =>
		     array(
		     	array('title' => KREDITOR_LANG_INFO_SE_IMGINVOICE,
					'field' => MODULE_PAYMENT_KREDITOR_TEXT_TITLE_CUST),
		    array('title' => KREDITOR_LANG_INFO_SE_CONDITIONS,
			    'field' => "<script type=\"text/javascript\" src=\"http://integration.klarna.com/js/klarnainvoice.js\"></script>
				<script>
					addKlarnaInvoiceEvent(function() { InitKlarnaInvoiceElements('klarna_invoice'," . MODULE_PAYMENT_KREDITOR_EID . ", 'se', " . round($kreditor_payment_chg, 2) . "); });
				</script>
				<a href=\"#\" id=\"klarna_invoice\" onclick=\"ShowKlarnaInvoicePopup();return false;\"></a>"),
			   	array('title' => KREDITOR_LANG_INFO_SE_PERSONALNUMBER,
				 'field' => zen_draw_input_field('kreditor_pnum',
				 	$personnummer != "" ? strftime("%y%m%d", strtotime($personnummer)) : "")),
			   	array('title' => KREDITOR_LANG_INFO_SE_PHONENUMBER,
					'field' => zen_draw_input_field('kreditor_phone',
						$order->customer['telephone'])),
			   	array('title' => KREDITOR_LANG_INFO_SE_EMAIL,
			   		'field' => zen_draw_input_field('kreditor_email',
						$order->customer['email_address']))));
    }

    function pre_confirmation_check() {
      require (DIR_WS_CLASSES . 'kreditor.php');
      global $order, $GA_OLD, $KRED_SE_PNO;
      
      $pno = $this->kreditor_pnum = $_POST['kreditor_pnum'];
      $eid = MODULE_PAYMENT_KREDITOR_EID;
      $secret = MODULE_PAYMENT_KREDITOR_SECRET;
      
      $pnoencoding = $KRED_SE_PNO;
      $type = $GA_OLD;
      
      $status = get_addresses($eid,$pno,$secret,$pnoencoding,$type,$result);
      $this->addrs = $result;
      if ($status != 0){
		$messageStack = new messageStack;
		$messageStack->add_session('checkout_payment', $result, 'error');
	zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT,
				   '',
				   'SSL', true, false));
      }
      
      if($result[0][0] == ""){
	$this->kreditor_fname = $result[0][1];
	$this->kreditor_lname = $result[0][0];
      }else{
	$this->kreditor_fname = $result[0][0];
	$this->kreditor_lname = $result[0][1];
      }
      $this->kreditor_fname = $result[0][0];
      $this->kreditor_lname = $result[0][1];
      $this->kreditor_street = $result[0][2];
      $this->kreditor_postno = $result[0][3];
      $this->kreditor_city = $result[0][4];
      $order->delivery['firstname'] = $this->kreditor_fname;
      $order->billing['firstname'] = $this->kreditor_fname;
      $order->delivery['lastname'] = $this->kreditor_lname;
      $order->billing['lastname'] = $this->kreditor_lname;
      $order->delivery['street_address'] = $this->kreditor_street;
      $order->billing['street_address'] = $this->kreditor_street;
      $order->delivery['postcode'] = $this->kreditor_postno;
      $order->billing['postcode'] = $this->kreditor_postno;
      $order->delivery['city'] = $this->kreditor_city;
      $order->billing['city'] = $this->kreditor_city;
      $this->kreditor_phone = $_POST['kreditor_phone'];
      $order->delivery['telephone'] = $this->kreditor_phone;
      $order->billing['telephone'] = $this->kreditor_phone;
      $this->kreditor_email = $_POST['kreditor_email'];
      $order->delivery['email_address'] = $this->kreditor_email;
      $order->billing['email_address'] = $this->kreditor_email;
    }

    function confirmation() {
	return false;
    }

    function process_button() {
	global $order, $order_total_modules, $kreditor_ot;
	$counter = 1;
	$a = count($this->addrs);
	  $process_button_string = "";
	if($a>1){
	  $process_button_string .= "<div align='left'>";
	  $process_button_string .=  "<table border='0' width='100%' cellspacing='1' cellpadding='2' class='infoBox'>";
	  $process_button_string .=  "<tr><td class='main'><b>".KREDITOR_LANG_INFO_SE_CHOOSEALTERNATIVES."</b></td></tr>";
	  $process_button_string .=  "<tr class='infoBoxContents'><td>";
	  
	  foreach ($this->addrs as $addr){
	    $this->kreditor_fname = $addr[0];
	    $this->kreditor_lname = $addr[1];
	    $this->kreditor_street = $addr[2];
	    $this->kreditor_postno = $addr[3];
	    $this->kreditor_city = $addr[4];
	    
	    if($counter == 1){
	      $checked = true;
	    }else{
	      $checked = false;
	    }
	    
	    $process_button_string .=
	       zen_draw_radio_field('addr_num', $counter, $checked, '').
	       zen_draw_hidden_field('kreditor_pnum'.$counter,
				     $this->kreditor_pnum).
	       zen_draw_hidden_field('kreditor_fname'.$counter,
				     $this->kreditor_fname).
	       zen_draw_hidden_field('kreditor_lname'.$counter,
				     $this->kreditor_lname).
	       zen_draw_hidden_field('kreditor_street'.$counter,
				     $this->kreditor_street).
	       $this->kreditor_street." ".
	       zen_draw_hidden_field('kreditor_postno'.$counter,
				     $this->kreditor_postno).
	       $this->kreditor_postno." ".
	       zen_draw_hidden_field('kreditor_city'.$counter,
				     $this->kreditor_city).
	       $this->kreditor_city." ".
	       zen_draw_hidden_field('kreditor_phone'.$counter,
				     $this->kreditor_phone).
	       zen_draw_hidden_field('kreditor_email'.$counter,
				     $this->kreditor_email)."<br/>";
	    $counter++;
	  }
	  $process_button_string .= "</td></tr></table><br/></div>";
	}else{
	  $process_button_string .=
	    zen_draw_hidden_field('addr_num', $counter, $checked, '').
	    zen_draw_hidden_field('kreditor_pnum'.$counter,
				  $this->kreditor_pnum).
	    zen_draw_hidden_field('kreditor_fname'.$counter,
				  $this->kreditor_fname).
	    zen_draw_hidden_field('kreditor_lname'.$counter,
				  $this->kreditor_lname).
	    zen_draw_hidden_field('kreditor_street'.$counter,
				  $this->kreditor_street).
	    zen_draw_hidden_field('kreditor_postno'.$counter,
				  $this->kreditor_postno).
	    zen_draw_hidden_field('kreditor_city'.$counter,
				  $this->kreditor_city).
	    zen_draw_hidden_field('kreditor_phone'.$counter,
				  $this->kreditor_phone).
	    zen_draw_hidden_field('kreditor_email'.$counter,
				  $this->kreditor_email);
	}

	$process_button_string .= zen_draw_hidden_field(zen_session_name(),
							zen_session_id());

	return $process_button_string;
    }

    function before_process() {
	global $order, $kreditor_ot;

	require(DIR_WS_CLASSES . 'kreditor.php');

	$eid = MODULE_PAYMENT_KREDITOR_EID;
	$estoreUser = $_SESSION['customer_id'];
	
	$goodsList = array();
	$n = sizeof($order->products);

	// First all the ordinary items
	for ($i = 0 ; $i < $n ; $i++) {
//	    $price_without_tax = ($order->products[$i]['final_price'] * 100/
//				  (1+$order->products[$i]['tax']/100));
//
	$price_with_tax = $order->products[$i]['final_price'] * $order->info['currency_value']*($order->products[$i]['tax']/100+1) * 100;

	    if (MODULE_PAYMENT_KREDITOR_ARTNO == 'id' || MODULE_PAYMENT_KREDITOR_ARTNO == '') {
		$goodsList[] =
		    mk_goods_flags($order->products[$i]['qty'],
			     zen_get_prid($order->products[$i]['id']),
			     $order->products[$i]['name'],
			     $price_with_tax,
			     $order->products[$i]['tax'],
			     0, 32);
	    } else {
		$goodsList[] =
		    mk_goods_flags($order->products[$i]['qty'],
			     $order->products[$i][MODULE_PAYMENT_KREDITOR_ARTNO],
			     $order->products[$i]['name'],
			     $price_with_tax,
			     $order->products[$i]['tax'],
			     0, 32);
	    }
	}
	
	
	// Then the extra charges like discount, invoicefee and shipping.
	foreach($GLOBALS['order_totals'] as $k_global => $v_global)
	{
	
		if(is_int(strpos(strtolower($v_global['code']),'discount')) || is_int(strpos(strtolower($v_global['title']),'discount'))
			|| is_int(strpos(strtolower($v_global['code']),'coupon')) || is_int(strpos(strtolower($v_global['title']),'coupon')))
		{
			$tod_rate = zen_get_tax_rate(MODULE_PAYMENT_KREDITOR_TAX_CLASS);
			$name = strip_tags($v_global['title']);
			$value = -$v_global['value'];
			$tax = $tod_rate*1;
			$name = str_replace(":", "", $name);
			$price_without_tax = $value*$order->info['currency_value']*100;
			if (DISPLAY_PRICE_WITH_TAX == 'true') {
				$price_without_tax = ($price_without_tax/(1+$tax/100));
			}
			if ($value != "" && $value != 0) 
				$goodsList[] = mk_goods(1, "",
							$name,
							$price_without_tax, $tax, 0);
		}
		
		if(is_int(strpos(strtolower($v_global['code']),'charge')) || is_int(strpos(strtolower($v_global['title']),'charge'))
			|| is_int(strpos(strtolower($v_global['code']),'payment')) || is_int(strpos(strtolower($v_global['title']),'payment'))
			|| is_int(strpos(strtolower($v_global['code']),'fee')) || is_int(strpos(strtolower($v_global['title']),'fee')))
		{
			$tod_rate = zen_get_tax_rate(MODULE_PAYMENT_KREDITOR_TAX_CLASS);
			$name = strip_tags($v_global['title']);
			$value = $v_global['value'];
			$tax = $tod_rate*1;                        
			$name = str_replace(":", "", $name);
			if(DISPLAY_PRICE_WITH_TAX == 'true') {
			$price_with_tax = $order->info['currency_value'] * $value * 100;
	} else {
	
		$price_with_tax = $order->info['currency_value'] * $value * 100*(($tax/100)+1);
		}
			if ($value != "" && $value != 0) 
				$goodsList[] = mk_goods_flags(1, "", $name, $price_with_tax, $tax, 0, 32);
		}
	}
	
	if (zen_not_null($order->info['shipping_method'])) {
	    $price_without_tax = $order->info['shipping_cost']*
	      $order->info['currency_value']*100;
	    $tod_rate = zen_get_tax_rate(MODULE_PAYMENT_KREDITOR_TAX_CLASS);
	    $tax = $tod_rate*1;

			if (DISPLAY_PRICE_WITH_TAX == 'false') {
				$price_without_tax = ($price_without_tax*(1+$tax/100));
			}
				$goodsList[] = mk_goods_flags(1, "",
							$order->info['shipping_method'],
							$price_without_tax, $tax, 0, 40);
	}

	$secret = MODULE_PAYMENT_KREDITOR_SECRET;
	$estoreOrderNo = "";
	$shipmentfee = 0;
	$shipmenttype = 1;

	$handlingfee = 0;
	$ready_date = "";

	// Fixes potential security problem
	$addr_num = $_POST['addr_num']; 
	$order->delivery['firstname'] = $_POST['kreditor_fname'.$addr_num];
	$order->billing['firstname'] =  $_POST['kreditor_fname'.$addr_num];
	$order->delivery['lastname'] =  $_POST['kreditor_lname'.$addr_num];
	$order->billing['lastname'] =   $_POST['kreditor_lname'.$addr_num];
	$order->delivery['street_address'] =$_POST['kreditor_street'.$addr_num];
	$order->billing['street_address'] = $_POST['kreditor_street'.$addr_num];
	$order->delivery['postcode'] = $_POST['kreditor_postno'.$addr_num];
	$order->billing['postcode'] = $_POST['kreditor_postno'.$addr_num];
	$order->delivery['city'] = $_POST['kreditor_city'.$addr_num];
	$order->billing['city'] = $_POST['kreditor_city'.$addr_num];
	$order->delivery['telephone'] = $_POST['kreditor_phone'.$addr_num];
	$order->billing['telephone'] = $_POST['kreditor_phone'.$addr_num];
	$order->delivery['email_address'] = $_POST['kreditor_email'.$addr_num];
	$order->billing['email_address'] = $_POST['kreditor_email'.$addr_num];

	$pno   = $_POST['kreditor_pnum'.$addr_num];
	$fname = $_POST['kreditor_fname'.$addr_num];
	$lname = $_POST['kreditor_lname'.$addr_num];
	$addr = mk_addr_se("",
			$_POST['kreditor_street'.$addr_num],
			$_POST['kreditor_postno'.$addr_num],
			$_POST['kreditor_city'.$addr_num],
			"",
			$_POST['kreditor_phone'.$addr_num],
			$_POST['kreditor_email'.$addr_num]);
	$passwd = "";
	$clientIp = $_SERVER["REMOTE_ADDR"];
	$newPasswd = "";

	$pno = str_replace("-", "", $pno);
	$pno = str_replace(" ", "", $pno);

if (MODULE_PAYMENT_KREDITOR_AUTO_ACTIVATE == 'True') {
            $flags = $AUTO_ACTIVATE ;
            if (MODULE_PAYMENT_KREDITOR_AUTO_ACTIVATE_SEND_DELAY > 0) {
                $ready_date =
                date('Y-m-d',
                    strtotime('now +' .
                        MODULE_PAYMENT_KREDITOR_AUTO_ACTIVATE_SEND_DELAY . ' days'));
            }
}			
	else
	    $flags = 0;

	$status =
	    add_transaction_se($eid, $estoreUser, $secret, $estoreOrderNo,
			$goodsList, $shipmentfee, $shipmenttype,
			$handlingfee, $pno, $fname, $lname, $addr,
			$passwd, $clientIp, $newPasswd, $flags,
			$comment, $ready_date, "", -1, 0, $result);


	if ($status == 0){
	    $order->kreditorref=$result; 
	    $payment['tan']=$result;
	} else {
		$messageStack = new messageStack;
		$messageStack->add_session('checkout_payment', $result, 'error');
	zen_redirect(zen_href_link(FILENAME_CHECKOUT_PAYMENT,
				   '',
				   'SSL', true, false));

	}
    }

    function after_process() {
	global $insert_id, $order, $db;

	$find_st_optional_field_query =
	    $db->Execute("show columns from " . TABLE_ORDERS);

	$has_kreditor_ref = false;

	while(!$find_st_optional_field_query->EOF) {
	    if ($find_st_optional_field_query->
		fields['Field'] == "kreditorref" ) 
		$has_kreditor_ref = true;
	    $find_st_optional_field_query->MoveNext();
	}

// 	while($fields = zen_db_fetch_array($find_st_optional_field_query)){
// 	    if ( $fields['Field'] == "kreditorref" ) 
// 		$has_kreditor_ref = true;
// 	}
	
	if ($has_kreditor_ref)
	    $db->Execute("update " . TABLE_ORDERS . " set kreditorref='" .
			 $order->kreditorref . "' " . " where orders_id = '" .
			 $insert_id . "'");

      
	// Insert transaction # into history file
	$sql_data_array = array('orders_id' => $insert_id, 
				'orders_status_id' => (
				    $order->info['order_status']), 
				'date_added' => 'now()', 
				'customer_notified' => 0,
				'comments' => ('Accepted by Klarna ' .
					       date("Y-m-d G:i:s") .
					       ' Invoice #: ' .
					       $order->kreditorref));

	zen_db_perform(TABLE_ORDERS_STATUS_HISTORY, $sql_data_array);

	$secret = MODULE_PAYMENT_KREDITOR_SECRET;
	$eid = MODULE_PAYMENT_KREDITOR_EID;
	$invno = $order->kreditorref;

	update_orderno($eid, $invno, $secret, utf8_decode($insert_id), $result);

	return false;

    }

    function get_error() {
	
	if (isset($_GET['error']) &&
	    strlen($_GET['error']) > 0) {
	    $error = stripslashes(urldecode($_GET['error']));
	} else {
	    $error = MODULE_PAYMENT_KREDITOR_TEXT_ERROR_MESSAGE;
	}

	return array('title' => MODULE_PAYMENT_KREDITOR_TEXT_ERROR,
		     'error' => $error);
    }

    function check() {
	global $db;

	if (!isset($this->_check)) {
	    $check_query = $db->Execute("select configuration_value from " .
					TABLE_CONFIGURATION .
					" where configuration_key = " .
					"'MODULE_PAYMENT_KREDITOR_STATUS'");
            $this->_check = $check_query->RecordCount();
	}
	return $this->_check;
    }

    function install() {
        global $db;
	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Swedish  Module', 'MODULE_PAYMENT_KREDITOR_STATUS', 'True', 'Do you want to accept Swedish Klarna payments?', '6', '0', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");

	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Payment Zone', 'MODULE_PAYMENT_KREDITOR_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '2', 'zen_get_zone_class_title', 'zen_cfg_pull_down_zone_classes(', now())");
      
 	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Merchant ID', 'MODULE_PAYMENT_KREDITOR_EID', '0', 'Merchant ID (estore id) to use for the Klarna service (provided by Klarna)', '6', '0', now())");

 	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Shared secret', 'MODULE_PAYMENT_KREDITOR_SECRET', '', 'Shared secret to use with the Klarna service (provided by Klarna)', '6', '0', now())");

 	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Auto Activate', 'MODULE_PAYMENT_KREDITOR_AUTO_ACTIVATE', 'False', 'Do you want to enable invoice auto activate?', '6', '0', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");
 	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Product artno attribute (id or model)', 'MODULE_PAYMENT_KREDITOR_ARTNO', 'id', 'Product attribute to use for artno', '6', '0', 'zen_cfg_select_option(array(\'id\', \'model\'), ', now())");

 	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Pre-populate personnummer field', 'MODULE_PAYMENT_KREDITOR_PRE_POPULATE', 'False', 'Do you want to pre-populate the personnummer field?', '6', '0', 'zen_cfg_select_option(array(\'True\', \'False\'), ', now())");

	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Ignore table', 'MODULE_PAYMENT_KREDITOR_ORDER_TOTAL_IGNORE', 'ot_tax,ot_total,ot_subtotal,ot_shipping', 'Ignore these entries from order total list when compiling the invoice data', '6', '2', now())");

	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Credit limit', 'MODULE_PAYMENT_KREDITOR_ORDER_LIMIT', '2500', 'Only show this payment alternative for orders less than the value below.', '6', '2', now())");

	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_PAYMENT_KREDITOR_SORT_ORDER', '21', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
      
	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Set Order Status', 'MODULE_PAYMENT_KREDITOR_ORDER_STATUS_ID', '0', 'Set the status of orders made with this payment module to this value', '6', '0', 'zen_cfg_pull_down_order_statuses(', 'zen_get_order_status_name', now())");

 	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Extra info', 'MODULE_PAYMENT_KREDITOR_TEXT_TITLE_CUST', '', 'Extra info, eg &lt;strong>OBS!&lt;/strong> invoice charge will be added 25:-', '6', '0', now())");

 	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Terms of payment', 'MODULE_PAYMENT_KREDITOR_TERMS', '".KREDITOR_LANG_INFO_SE_PAYMENT."', '', '6', '0', now())");

	$db->Execute("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_PAYMENT_KREDITOR_TAX_CLASS', '0', 'Use the following tax class on the payment charge.', '6', '6', 'zen_get_tax_class_title', 'zen_cfg_pull_down_tax_classes(', now())");

    }

    function remove() {
	global $db;
	$db->Execute("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
	return array('MODULE_PAYMENT_KREDITOR_STATUS',
		     'MODULE_PAYMENT_KREDITOR_ORDER_STATUS_ID',
		     'MODULE_PAYMENT_KREDITOR_EID',
		     'MODULE_PAYMENT_KREDITOR_SECRET',
		     'MODULE_PAYMENT_KREDITOR_AUTO_ACTIVATE',
	         'MODULE_PAYMENT_KREDITOR_ARTNO',
	         'MODULE_PAYMENT_KREDITOR_PRE_POPULATE',
		     'MODULE_PAYMENT_KREDITOR_ORDER_LIMIT',
		     'MODULE_PAYMENT_KREDITOR_ORDER_TOTAL_IGNORE',
		     'MODULE_PAYMENT_KREDITOR_ZONE',
		     'MODULE_PAYMENT_KREDITOR_TERMS',
		     'MODULE_PAYMENT_KREDITOR_TAX_CLASS',
		     'MODULE_PAYMENT_KREDITOR_TEXT_TITLE_CUST',
		     'MODULE_PAYMENT_KREDITOR_SORT_ORDER');
    }
}
?>
